#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      patrick
#
# Created:     16/05/2012
# Copyright:   (c) patrick 2012
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python


from moddt8837 import *
import pylab as plt
import time
import numpy as np
import scipy as sc
import struct
import time


def raw2volt(rawValue,gain):
    """
    Vpeak-to-peak=2*Vpeak
    Vrms=Vpp/(2*sqrt(2))
    """
    RESOLUTION    = pow(2,24)
    VOLT_MIN_1    = -1.0      # Minimum voltage x10 gain
    VOLT_MAX_1    = 1.0       # Maximum voltage x10 gain
    PERCENT_FS    = (VOLT_MAX_1 - VOLT_MIN_1)/RESOLUTION
    volts = (rawValue * PERCENT_FS) + VOLT_MIN_1
    volts=volts*2.0*pow(2,0.5)
##    if (gain == 1):  # gain 1
##        volts *= 10.0
    return volts

DEV1 = "TCPIP::169.254.241.19::INSTR";
#DEV2 ='TCPIP::169.254.241.19::INSTR';

#-------------------------------------------------------------------------------
# reccupere le handle des boitiers
#-------------------------------------------------------------------------------
h1 = getHandle(DEV1)
#h2 = getHandle(DEV2)

#-------------------------------------------------------------------------------
# lecture du nom des boitires, identification
#-------------------------------------------------------------------------------
s1=getID(h1)
#s2=getID(h2)
print s1



#-------------------------------------------------------------------------------
# reset des modules
#-------------------------------------------------------------------------------
print "Reset des modules..."
err1 = doRST(h1)
##err2 = doRST(h2)
h1.write("AD:STATUS:SCAN?")
print h1.read()
h1.write("AD:STATUS?")
print h1.read()


#-------------------------------------------------------------------------------
# Enable AD for all channel
#-------------------------------------------------------------------------------
err = h1.write(":AD:ENAB OFF;:AD:BUFF:SIZe?")
print "taille buffer acquisition = ", h1.read()
print "Activation des  modules AD..."
err1 = setAdEnableAll(h1)
##err2 = setAdEnableAll(h2)

#-------------------------------------------------------------------------------
# couplage AC/DC pour tous les modules
#-------------------------------------------------------------------------------
print "Couplage  des  voies en AC ..."
err1 = setCouplage(h1,'AC')
##err2 = setCouplage(h2,'AC')

#-------------------------------------------------------------------------------
# mode acquisition du buffer wrap/no wrap
#-------------------------------------------------------------------------------
print "Mode acquisition buffer wrap/no wrap ..."
err1 = setWrap(h1,"on")  #   buffer circulaire
##err1 = setWrap(h2,"on")

#-------------------------------------------------------------------------------
# initialisation des gains pour toutes les voies  1/10
#-------------------------------------------------------------------------------
print "Initialisation des gains ..."
level=1   # 1 = 10V en entree  ,10=1V en entree
err1 = setGain(h1,level)
##err2 = setGain(h2,level)

#-------------------------------------------------------------------------------
# horloge interne
#-------------------------------------------------------------------------------
print "Initialisation horloge interne ..."
err1=setADClockInternal(h1)
##err2=setADClockInternal(h2)

#-------------------------------------------------------------------------------
# frequence horloge interne
#-------------------------------------------------------------------------------
print "Initialisation frequence horloge interne ..."
fs=5000   # 10000 Hz
err1=setFreqClock(h1,fs)
##err2=setFreqClock(h2,fs)
dt=1.0/fs    #periode echantillonnange
#-------------------------------------------------------------------------------
# trigger immediat / software
#-------------------------------------------------------------------------------
print "Initialisation trigger source immediat ..."
err1=setAdTrigSourceImm(h1)
#err2=setAdTrigSourceImm(h2)

#-------------------------------------------------------------------------------
# pret a lancer la mesure
#-------------------------------------------------------------------------------
print "Armement des mesures, pret a lancer ..."
err1=setAdArm(h1)
#err2=setAdArm(h2)

#-------------------------------------------------------------------------------
# lance la mesure la mesure
#-------------------------------------------------------------------------------
print "lance la mesure ...."
err1=setAdInit(h1)
#err2=setAdInit(h2)



tacq=2 # temps acquisition 1s
##print "attente ..."
buffersize=tacq/dt # nombre d echantillon pour tacq
##print "nb. element : ", buffersize

N=254 #254  # work well only with 100 !!!!!!!!!!!!!!!!! or less than 255 (254)


nbblock=int(buffersize/N-1)
##print "nb. de blocs : ",nbblock
time.sleep(tacq)  #attente 2s

#-------------------------------------------------------------------------------
# arrete la mesure la mesure
#-------------------------------------------------------------------------------
err1=setAdStop(h1)
#err2=setAdStop(h2)


#-------------------------------------------------------------------------------
# on reccupere les donnees
#-------------------------------------------------------------------------------
t0= time.clock()

mess=":AD:FETCH? 0,"+str(N-1)
##print "---------------------------"
##print mess
err1=h1.write (mess)
data=h1.read_raw()

v0,v1,v2,v3=[],[],[],[]

sizeHeader,sizeBuffer,header,buf0,buf1,buf2,buf3 =  readHeader(data)
##print sizeHeader,sizeBuffer
##print header

for i in range(1,nbblock):
##    print "---------------------------"
    mess=":AD:FETCH? "+str(i*N) +","+str(N)
##    print mess
    err1=h1.write (mess)
##    print "retour fetch = ",err1
    data1=h1.read_raw()
    sizeHeader,sizeBuffer,header,buf0,buf1,buf2,buf3 =  readHeader(data1)
##    print sizeHeader,sizeBuffer
##    print header
    #
    v0=np.concatenate(( v0,buf0), axis=0)
    v1=np.concatenate(( v1,buf1), axis=0)
    v2=np.concatenate(( v2,buf2), axis=0)
    v3=np.concatenate(( v3,buf3), axis=0)


v0=raw2volt(v0,level)
v1=raw2volt(v1,level)
v2=raw2volt(v2,level)
v3=raw2volt(v3,level)
t= time.clock() - t0 # t is wall seconds elapsed (floating point)
print "duree = ",t

print "arret de la mesure ...."
plt.figure(1)
plt.hold(True)
plt.subplot(411)
plt.plot(v0,'k')
plt.subplot(412)
plt.plot(v1,'b')
plt.subplot(413)
plt.plot(v2,'r')
plt.subplot(414)
plt.plot(v3,'g')
##fic = open("sinus.dat","wb")
##fic.write(data)
##fic.close()
plt.show()
exit(0)






